package com.hjx.windowtool.bk;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.stream.Collectors;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;


import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class ReadApp {

	private static String charset = "UTF-8";
	protected Shell shell;
	private Text txt_content;
	private List list;
	private String content;
	private java.util.List<String> listTitle;
	private String selectTitle;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("第001章 大萨达多".matches(".*([0-9]+|[一二三四五六七八九十百千]+)[章节]+.*"));
		try {
			ReadApp window = new ReadApp();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 384);
		shell.setText("约");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("菜单");
		
		Menu menu_1 = new Menu(menuItem);
		menuItem.setMenu(menu_1);
		
		MenuItem menuItem_1 = new MenuItem(menu_1, SWT.NONE);
		menuItem_1.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog f = new FileDialog(shell);
				String open = f.open();
				if (open==null) {
					return;
				}
				System.out.println(open);
				try {
					byte[] readAllBytes = Files.readAllBytes(new File(open).toPath());
					System.out.println((readAllBytes[0]&0xff)+"==========="+(readAllBytes[1]&0xff)+"==========="+(readAllBytes[2]&0xff));
					java.util.List<String> listTxt = Files.readAllLines(new File(open).toPath(), Charset.forName(charset));
					listTitle = listTxt.stream().filter(e1->e1.matches(".*([0-9]+|[一二三四五六七八九十百千]+)[章节集]+.*")).collect(Collectors.toList());
					list.setItems(listTitle.toArray(new String[listTitle.size()]));
					StringBuffer sb=new StringBuffer();
					for (String string : listTxt) {
						sb.append(string+"\r\n");
					}
					content=sb.toString();
					String string = listTitle.get(0);
					String substring = sb.substring(0, sb.indexOf(string));
					txt_content.setText(substring);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		menuItem_1.setText("打开");
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(2, false);
		gl_composite_1.marginHeight = 0;
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.marginWidth = 0;
		gl_composite_1.horizontalSpacing = 0;
		composite_1.setLayout(gl_composite_1);
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_composite_1.widthHint = 162;
		composite_1.setLayoutData(gd_composite_1);
		
		Composite composite_3 = new Composite(composite_1, SWT.NONE);
		composite_3.setLayout(new FillLayout(SWT.HORIZONTAL));
		GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		composite_3.setLayoutData(layoutData);
		
		list = new List(composite_3, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				String[] strings = list.getSelection();
				if (strings==null||strings.length<=0) {
					return;
				}
				selectTitle=strings[0];
				int index = content.indexOf(strings[0]);
				int i = listTitle.indexOf(strings[0]);
				int endIndex=content.length();
				if (i<listTitle.size()-1) {
					endIndex=content.indexOf(listTitle.get(i+1));
				}
				txt_content.setText(content.substring(index, endIndex));
			}
		});
		
		ToolBar toolBar = new ToolBar(composite_1, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		
		ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
		toolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				layoutData.exclude=true;
				composite_3.setLayoutData(layoutData);
				composite_3.getParent().layout();
				composite_3.layout();
			}
		});
		toolItem.setText("<");
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.marginWidth = 0;
		gl_composite_2.marginHeight = 0;
		composite_2.setLayout(gl_composite_2);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		txt_content = new Text(composite_2, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		txt_content.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				System.out.println(e.keyCode);
				if (16777220==e.keyCode) {
					nextTitle();
				}
				else if(16777219==e.keyCode) {
					lastTitle();
				}
			}
		});
		txt_content.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		txt_content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		txt_content.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		
		Label lbl_pageSize = new Label(composite_2, SWT.NONE);
		lbl_pageSize.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

	}

	protected void nextTitle() {
		try {
			int i = listTitle.indexOf(selectTitle);
			int endIndex=content.length();
			if (i<listTitle.size()-1) {
				endIndex=content.indexOf(listTitle.get(i+2));
			}
			int index = content.indexOf(listTitle.get(i+1));
			selectTitle=listTitle.get(i+1);
			txt_content.setText(content.substring(index, endIndex));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void lastTitle() {
		try {
			int i = listTitle.indexOf(selectTitle);
			int endIndex=content.length();
			if (i<listTitle.size()-1) {
				endIndex=content.indexOf(selectTitle);
			}
			int index = content.indexOf(listTitle.get(i-1));
			txt_content.setText(content.substring(index, endIndex));
			selectTitle=listTitle.get(i+1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
